import resizeViewConfig from '@/utils/commonUtils/resizeViewConfig'// 全局更改大小
import { getList, saveOrUpdate, removeIdsApi } from '@/api/publicApi'
// 混入
import { publicMixins } from '@/mixins/publicMixins/publicMixins'
// dialogListMixins
export const dialogListMixins = {
  mixins: [publicMixins],
  data() {
    return {
    // 是否显示弹出层
      dialogListVisibleBool: false,
      // 遮罩层
      dialogListLoading: false,
      // 弹窗title
      dialogListTitle: '',
      // 弹窗传值 额外入参
      dialogListExtra: {},
      // 选择类型 单选：radio,复选：checkbox 默认：checkbox
      dialogListSelectBox: 'checkbox',
      // 表格数据
      dialogList: [],
      // 总数
      dialogListTotal: 0,
      // 选择的数据临时数组
      dialogListSelectTableList: [],
      // 单选时的勾选的临时数据
      dialogListSelectIdRowId: null
    }
  },
  watch: {
    dialogListVisibleBool(a, b) {
      this.$nextTick(() => {
        resizeViewConfig()
      })
    }
  },

  mounted() {

  },
  methods: {
    resizeViewConfigFun() {
      this.$nextTick(() => {
        resizeViewConfig()
      })
    },
    /**
     * @description: 获取列表弹窗数据
     * @Author: baoxuhui
     * @param {*} url//联调的后端接口路径
     * @param {*} queryParams//接口所需的入参
     * @param {*} pageBool//是否分页(布尔值)   默认：true
     * @param {*} returnBool//是否回调(布尔值)   默认：false
     * @return {*}
     */
    dialogGetListMixins(url, queryParams, pageBool, returnBool) {
      let newData = []
      let newPageBool = true
      if (this.sentencedEmpty(pageBool)) {
        newPageBool = pageBool
      }
      this.dialogListLoading = true
      return new Promise((resolve) => {
        getList(url, queryParams).then(response => {
          if (newPageBool) {
            newData = response.rows
            this.dialogListTotal = response.total
          } else {
            newData = response.data
          }
          if (this.dialogListSelectTableList.length > 0) {
            newData.forEach(ele => {
              this.dialogListSelectTableList.map(item => {
                if (ele.id === item.id) {
                  setTimeout(() => {
                    this.$refs['dialogList'].toggleRowSelection(ele, true)
                  }, 10)
                }
              })
            })
          }
          if (!returnBool) {
            this.dialogList = newData
            this.dialogListLoading = false
          }

          resolve(newData)
        })
      })
    },
    /**
     * @description: :保存列表弹框走接口事件（新增/修改）
     * @Author: baoxuhui
     * @param {*} url//请求路径
     * @param {*} data//入参
     * @param {*} type//保存/修改.......
     * @return {*}
     */
    dialogSaveOrUpdateListMixins(url, data, type) {
      return new Promise((resolve) => {
        this.dialogFormLoading = true
        saveOrUpdate(url, data).then(res => {
          this.msgAlt(type + '成功', 'success')
          resolve(res)
        }).catch(() => {
          this.dialogFormLoading = false
        })
      })
    },
    /**
     * @description: 取消列表弹框事件
     * @Author: baoxuhui
     * @return {*}
     */
    dialogResetListMixins() {
      this.dialogListVisibleBool = false
      this.dialogListLoading = false
      this.dialogListSelectTableList = []
      this.$refs['dialogList'].clearSelection()
      if (this.listQueryParams.$refs['advancedQueryShrinkComponent']) {
        this.listQueryParams.$refs['advancedQueryShrinkComponent'].handleResetFun()
      }
      if (this.$refs['advancedQueryShrinkComponent']) {
        this.$refs['advancedQueryShrinkComponent'].handleResetFun()
      }
    },
    /**
     * @description: 删除事件
     * @Author: baoxuhui
     * @param {*} url//接口路径
     * @param {*} queryParams//入参
     * @param {*} paData//弹窗中父级保留值
     * @return {*}
     */
    dialogDeleteMixins(url, queryParams, paData, text) {
      const newText = text || '确认删除吗?'
      this.$confirm(newText, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return removeIdsApi(url, queryParams)
      }).then(() => {
        // if (this.$refs['advancedQueryShrinkComponent']) {
        //   this.$refs['advancedQueryShrinkComponent'].handleResetFun()
        // } else {
        this.dialogGetList(paData)
        // }
        this.msgAlt('删除成功', 'success')
      })
    }
  }

}
